/**
 * Program  : ParseXmlUtil.java
 * Author   : qianj
 * Create   : 2012-4-11 下午03:58:45
 *
 * Copyright 2012 by newyulong Technologies Ltd.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of newyulong Technologies Ltd.("Confidential Information").  
 * You shall not disclose such Confidential Information and shall 
 * use it only in accordance with the terms of the license agreement 
 * you entered into with newyulong Technologies Ltd.
 *
 */

package com.newyulong.implatform.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;

import android.content.Context;
import android.util.Xml;

/**
 * 解析
 * 
 * @author qianj
 * @version 1.0.0
 * @2012-4-11 下午03:58:45
 */
public class ParseXmlUtil {

	/**
	 * 得到地区的编码
	 * 
	 * @author qianj
	 * @create 2012-4-11 下午04:20:26
	 * @since
	 * @param xmlStr
	 * @return
	 */
	public static String getRegionCode(String xmlStr) {
		String regionCode = "";
		XmlPullParser parser = Xml.newPullParser();
		InputStream is = new ByteArrayInputStream(xmlStr.getBytes());
		try {
			parser.setInput(is, "gbk");
			int eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				String tagName = parser.getName();
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:

					break;
				case XmlPullParser.START_TAG:
					if ("location".equals(tagName)) {
						regionCode = parser.nextText();
					}
					break;
				case XmlPullParser.END_TAG:

					break;
				case XmlPullParser.END_DOCUMENT:

					break;

				default:
					break;
				}
			}
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return regionCode;
	}

	/**
	 * 得到地区的拼音简写
	 * 
	 * @author qianj
	 * @create 2012-4-11 下午05:49:39
	 * @since
	 * @param key
	 * @return
	 */
	public static String getRegionPy(String key, Context context) {
		InputStream inputStream = context.getClass().getClassLoader()
				.getResourceAsStream("sh_config.xml");
		XmlPullParser parser = Xml.newPullParser();
		String value = "";
		try {
			parser.setInput(inputStream,"utf-8");
			int eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				String tagName = parser.getName();
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:

					break;
				case XmlPullParser.START_TAG:
					if ("string".equals(tagName)) {
						if(parser.getAttributeValue(0).equals(key)){
							value = parser.nextText();
						}
					}
					break;
				case XmlPullParser.END_TAG:

					break;
				case XmlPullParser.END_DOCUMENT:

					break;

				default:
					break;
				}
			}
			inputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return value;

	}

}
